package com.ignou.mca.evote;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.datasource.DataBaseUtilities;
import com.ignou.mca.datasource.Encryption;
import com.ignou.mca.beans.Message;
import com.ignou.mca.beans.PartiesFormBean;
import com.ignou.mca.util.SessionUtil;

public class AddParties extends HttpServlet implements Servlet {

	DataBaseUtilities dbu = new DataBaseUtilities();
	Connection con = null;
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String partyName = req.getParameter("partyName");
		String partyDescription = req.getParameter("partyDescription");
		String address1 = req.getParameter("address1");
		String address2 = req.getParameter("address2");
		String city = req.getParameter("city");
		String state = req.getParameter("state");
		String country = req.getParameter("country");
		String phno = req.getParameter("phno");
		String website = req.getParameter("website");

		Message message = new Message();
		PartiesFormBean party = new PartiesFormBean();
		party.setPartyName(partyName);
		party.setPartyDescription(partyDescription);
		party.setAddress1(address1);
		party.setAddress2(address2);
		party.setCity(city);
		party.setState(state);
		party.setCountry(country);
		party.setPhno(phno);
		party.setWebsite(website);

		HttpSession session = req.getSession(true);
		session.setAttribute("Message", message);
		req.setAttribute("Party", party);
		ServletContext context = getServletContext();

		dbu = new DataBaseUtilities();
		
		if (!SessionUtil.validateSession(req, resp)) {
			resp.sendRedirect(req.getServletPath()+"/Login.jsp");
			return;
		}

		if ((partyName.equalsIgnoreCase("")) || (address1.equalsIgnoreCase(""))
				|| (address2.equalsIgnoreCase(""))
				|| (city.equalsIgnoreCase("")) || (state.equalsIgnoreCase(""))
				|| (phno.equalsIgnoreCase(""))) {
			message.setMessage("You have not completed all the fields.Please complete all mandatory fields.");
			context.getRequestDispatcher("/AddParties.jsp").forward(req,
					resp);
		} else {

			if(con==null) con = dbu.getConnection();
			String query = "insert into parties(PartyName,Description,Address1,"
					+ "Address2,city,state,country,phone) values(?,?,?,?,?,?,?,?)";

			if (checkParty(party.getPartyName())) {
				req.setAttribute("ErrorMessage",
				"Party already added in database.");
				context.getRequestDispatcher("/TransactionFailed.jsp").forward(
						req, resp);
			}else{

				try {
					PreparedStatement pst = con.prepareStatement(query);
					pst.setString(1, party.getPartyName());
					pst.setString(2, party.getPartyDescription());
					pst.setString(3, party.getAddress1());
					pst.setString(4, party.getAddress2());
					pst.setString(5, party.getCity());
					pst.setString(6, party.getState());
					pst.setString(7, party.getCountry());
					pst.setString(8, party.getPhno());
	
					int i = pst.executeUpdate();
	
					if (i == 1) {
						context.getRequestDispatcher("/AdminMenu.jsp")
						.forward(req, resp);
					}else
					{
						req.setAttribute("ErrorMessage",
						"Error in connecting to database. Please try again later.");
						context.getRequestDispatcher("/TransactionFailed.jsp").forward(
								req, resp);	
					}
	
				} catch (Exception e) {
	
				}
			}

		}
	}

	public boolean checkParty(String partyName) {
		if(con==null) con = dbu.getConnection();
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from parties where PartyName='"+ partyName+"'");

			while (rs.next()) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}
}